package com.have.blog.xo.service.admin.impl;

import com.have.blog.base.enums.BlogStatus;
import com.have.blog.commons.feign.PictureFeignClient;
import com.have.blog.model.mapper.TWebConfigMapper;
import com.have.blog.model.models.TWebConfig;
import com.have.blog.model.models.TWebConfigExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.constant.SysConstant;
import com.have.blog.xo.service.admin.WebConfigService;
import com.have.blog.xo.uitls.WebUtil;
import com.have.blog.xo.vo.WebConfigVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
@SuppressWarnings("all")
public class WebConfigServiceImpl implements WebConfigService {
    @Autowired
    private TWebConfigMapper webConfigMapper;
    @Autowired
    private PictureFeignClient pictureFeignClient;
    @Autowired
    private WebUtil webUtil;


    @Override
    public String getWebConfig() {
        TWebConfigExample example = new TWebConfigExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("create_time desc");
        List<TWebConfig> webConfigs = webConfigMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(webConfigs)) {
            return ResultUtils.errorWithMessage(MessageConstant.ENTITY_NOT_EXIST);
        }
        TWebConfig webConfig = webConfigs.get(0);

        //获取图片
        if (webConfig != null && !StringUtils.isEmpty(webConfig.getLogo())) {
            String pictureList = this.pictureFeignClient.getPicture(webConfig.getLogo(), SysConstant.FILE_SEGMENTATION);
            webConfig.setPhotoList(webUtil.getPicture(pictureList));
        }

        //获取支付宝收款二维码
        if (webConfig != null && !StringUtils.isEmpty(webConfig.getAliPay())) {
            String pictureList = this.pictureFeignClient.getPicture(webConfig.getAliPay(), SysConstant.FILE_SEGMENTATION);
            if (webUtil.getPicture(pictureList).size() > 0) {
                webConfig.setAliPayPhoto(webUtil.getPicture(pictureList).get(0));
            }

        }
        //获取微信收款二维码
        if (webConfig != null && !StringUtils.isEmpty(webConfig.getWeixinPay())) {
            String pictureList = this.pictureFeignClient.getPicture(webConfig.getWeixinPay(), SysConstant.FILE_SEGMENTATION);
            if (webUtil.getPicture(pictureList).size() > 0) {
                webConfig.setWeixinPayPhoto(webUtil.getPicture(pictureList).get(0));
            }
        }
        return ResultUtils.successWithData(webConfig);

    }

    @Override
    public String getWebSiteName() {
        TWebConfigExample example = new TWebConfigExample();
        List<TWebConfig> tWebConfigs = webConfigMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(tWebConfigs)) {
            return ResultUtils.successWithData(tWebConfigs.get(0).getName());
        }
        return ResultUtils.successWithData("");
    }

    @Override
    public TWebConfig getWebConfigByShowList() {
        return null;
    }

    @Override
    public String editWebConfig(WebConfigVo webConfigVO) {
        int result = 0;
        if (StringUtils.isEmpty(webConfigVO.getUid())) {
            TWebConfig webConfig = new TWebConfig();
            // 设置网站配置【使用Spring工具类提供的深拷贝】
            BeanUtils.copyProperties(webConfigVO, webConfig, SysConstant.STATUS);
            webConfig.setUid(MyStringUtils.getUUID());
            webConfig.setStatus(BlogStatus.ENABLE.getStatus());
            result = webConfigMapper.insert(webConfig);
        } else {
            TWebConfig webConfig = webConfigMapper.selectByPrimaryKey(webConfigVO.getUid());
            // 更新网站配置【使用Spring工具类提供的深拷贝】
            BeanUtils.copyProperties(webConfigVO, webConfig, SysConstant.STATUS, SysConstant.UID);
            webConfig.setUpdateTime(new Date());
            result = webConfigMapper.updateByPrimaryKeySelective(webConfig);

            // TODO 修改配置后，清空Redis中的 WEB_CONFIG
            // 同时清空Redis中的登录方式
        }

        if (result <= 0) {
            return ResultUtils.errorWithMessage(MessageConstant.UPDATE_FAIL);
        }
        return ResultUtils.successWithMessage(MessageConstant.UPDATE_SUCCESS);
    }

    @Override
    public Boolean isOpenLoginType(String loginType) {
        return null;
    }
}
